package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.entity.User;
import com.qk.dto.user.UserDTO;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 条件分页查询用户列表
     */
    @GetMapping
    public Result getUsers(UserDTO userDto) {
        PageResult<UserDO> userPage = userService.getUsers(userDto);
        return Result.success(userPage);
    }



    /**
     * 新增用户
     *
     * @param user 用户信息
     * @return 统一响应结果
     */
    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户: {}", user);
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 批量删除用户
     *
     * @param ids 用户ID数组
     * @return 统一响应结果
     */
    @DeleteMapping("/{ids}")
    public Result deleteUsers(@PathVariable("ids") List<Integer> ids) {
        log.info("批量删除用户: {}", ids);
        userService.deleteUsers(ids);
        return Result.success();
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id 用户ID
     * @return 统一响应结果
     */
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        log.info("根据ID查询用户: {}", id);
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     *
     * @param user 用户信息
     * @return 统一响应结果
     */
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.updateUser(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAllUsers() {
        log.info("查询所有用户数据");
        List<User> users = userService.listAllUsers();
        return Result.success(users);
    }

    @GetMapping("/dept/{deptId}")
    public Result getUsersByDeptId(@PathVariable Integer deptId) {
        log.info("根据部门ID查询用户列表，部门ID: {}", deptId);
        List<User> users = userService.getUsersByDeptId(deptId);
        return Result.success(users);
    }

    // 新增根据角色标签查询用户的接口
    @GetMapping("/role/{roleLabel}")
    public Result getUsersByRoleLabel(@PathVariable String roleLabel) {
        log.info("根据角色标签查询用户列表，角色标签: {}", roleLabel);
        List<User> users = userService.getUsersByRoleLabel(roleLabel);
        return Result.success(users);
    }
}